{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "arabic-honey",
   "metadata": {},
   "source": [
    "# filter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebe4a7f0",
   "metadata": {},
   "source": [
    "Filters rows based on predicate on pandas data frame\n",
    "Example \"predicate=~metadata.filename.str.contains('.gz') \" => filters all rows where column \"filename\" contains '.gz\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fabulous-israeli",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d92abd54",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import logging\n",
    "logging.basicConfig(level=logging.DEBUG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "upset-affair",
   "metadata": {},
   "outputs": [],
   "source": [
    "# predicate (as described in documentation of the component)\n",
    "predicate = os.environ.get('predicate')\n",
    "\n",
    "# file name / path of the CSV file to read\n",
    "file_name = os.environ.get('file_name')\n",
    "\n",
    "# output file name / path of the CSV file to write\n",
    "output_file_name = os.environ.get('output_file_name')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "atmospheric-mauritius",
   "metadata": {},
   "outputs": [],
   "source": [
    "logging.debug('Opening file...')\n",
    "df = pd.read_csv(file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "excellent-conference",
   "metadata": {},
   "outputs": [],
   "source": [
    "logging.debug('Filtering...')\n",
    "exec('df = df[' + predicate + ']')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "parental-dialogue",
   "metadata": {},
   "outputs": [],
   "source": [
    "logging.debug('Writing file...')\n",
    "df.to_csv(output_file_name, index=False)\n",
    "logging.debug('Done')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
